#include "bc_utils.h"

using namespace std;

WMI_HResult  
bc_utils::initConnection(WMI_Connection& connection, wstring& user, wstring& password, 
						 wstring& host, bool connectionType, bool localActive)
{ 
    WMI_ConnectionManager* wmiMng = new WMI_ConnectionManager();

	WMI_HResult whres;

    if(connectionType)
    {
	    if(!localActive)
	    {			
		    whres = wmiMng->createConnection(user, password, host, PC_NAMESPACE_REG, connection);	
	    }
	    else
	    {
		    whres = wmiMng->createConnection(PC_LOCAL_USER, PC_LOCAL_PASSWORD, PC_NAMESPACE_REG, connection);	
	    }
    }
    else
    {
	    if(!localActive)
	    {
		    whres = wmiMng->createConnection(user, password, host, PC_NAMESPACE, connection);	
	    }
	    else
	    {
		    whres = wmiMng->createConnection(PC_LOCAL_USER, PC_LOCAL_PASSWORD, PC_NAMESPACE, connection);	
	    }
    }
	delete wmiMng;
    return whres;
}

void 
bc_utils::getProcess(WMI_Connection& wmiConnection,CM_ResultCollection& resultCollection)
{

    while(true)
    {
		WMI_HResult whres;
	    /*WMI_Connection wmiConnection;
	    WMI_HResult whres = initConnection(wmiConnection, user, password, host, connectionType, localActive);

	    if(FAILED(whres.getHRESULT() ))
	    {
			wcout << "Unable to connect to "<< host << endl;
		    break;
	    }*/

	    whres = wmiConnection.executeQuery(L"SELECT * FROM Win32_Process");

	    if( FAILED(whres.getHRESULT()))
	    {
		    break;
	    }

	    IEnumWbemClassObject* pEnumerator = NULL;
	    pEnumerator = wmiConnection.getIEnumWbemClassObject();
	   
	    WMI_Result<WMI_Process> listProcess;
	  //  listProcess.fillVector(pEnumerator);

		resultCollection.getProcesses().fillVector(pEnumerator);//setProcesses(listProcess);

	    //wmiConnection.close();

		break;
    }
}

void 
bc_utils::getShares(WMI_Connection& wmiConnection, CM_ResultCollection& resultCollection)
{
    while(true)
    {
		WMI_HResult whres;
	    /*WMI_Connection wmiConnection;
	    WMI_HResult whres = initConnection(wmiConnection, user, password, host, connectionType, localActive);

	    if(FAILED(whres.getHRESULT() ))
	    {
			wcout << "Unable to connect to "<< host << endl;
		    break;
	    }*/

	    whres = wmiConnection.executeQuery(L"SELECT * FROM Win32_Share");
	    if( FAILED(whres.getHRESULT()))
	    {
		    break;
	    }

	    IEnumWbemClassObject* pEnumerator = NULL;
	    pEnumerator = wmiConnection.getIEnumWbemClassObject();
	   
	    WMI_Result<WMI_Share> listShares;
	    //listSares.fillVector(pEnumerator);

		resultCollection.getShares().fillVector(pEnumerator);        
	    //wmiConnection.close();

		break;
    }
}

void 
bc_utils::getRegistries(WMI_Connection& wmiConnection, const wstring& path, CM_ResultCollection& resultCollection)
{

    /*WMI_Connection wmiConnection;
	
    WMI_HResult whres = initConnection(wmiConnection, user, password, host, connectionType, localActive);

    if(FAILED(whres.getHRESULT() ))
    {
	    wcout << "Unable to connect to "<< host << endl;
    }*/

    WMI_RegistryManager man(&wmiConnection);
    WMI_Registry* rclass = new WMI_Registry();

    wstring root;
    wstring subkey;

	man.getParseRoot(path, root, subkey);

    HiveEnum rootEnum = man.convertStringToHKEY(root);
    wstring subKeyReg;
    wstring outPath;
    man.getRegistryName(subkey, outPath, subKeyReg);

 
    man.getRegistrys(rootEnum, subKeyReg, &rclass);
	
    WMI_Result<WMI_Registry> registry;
    //registry.setWRegistry(rclass);
	
	resultCollection.getRegistries().setWRegistry(rclass);
    // wmiConnection.close();
}

void 
bc_utils::getFileDirectory(WMI_Connection& wmiConnection, const wstring& path, CM_ResultCollection& resultCollection)
{
	//WMI_Connection wmiConnection;
	//WMI_HResult whres = initConnection(wmiConnection, user, password, host, connectionType, localActive);

	//if(FAILED(whres.getHRESULT() ))
	//{
	//	wcout << "Unable to connect to "<< host << endl;
	//	return;
	//}


	WMI_FileDirManager dirMan(&wmiConnection);

	wstring fpath;
	wstring fileName;

	dirMan.getFileDirName(path, fpath, fileName);

	dirMan.getFileDirs(fpath, resultCollection.getFileDirs());	
}


void
bc_utils::fillDifferences(WMI_Result<WMI_FileDirectory>& currentItems, WMI_Result<WMI_FileDirectory>& beforeCurrentItems, WMI_Result<WMI_FileDirectory>& differences)
{
    for(int j = 0; j < currentItems.getObjects()->getContent().size(); j++)
    {
        for(int i = 0; i < beforeCurrentItems.getObjects()->getContent().size(); i++)
        {
            if((*(currentItems.getObjects()->getContent()[j])).getName() == (*(beforeCurrentItems.getObjects()->getContent()[i])).getName())
            {
				BC_Comparator<WMI_FileDirectory> comparator;
                if(comparator.compare(currentItems.getObjects()->getContent()[j], beforeCurrentItems.getObjects()->getContent()[i]))
                {
                    differences.getObjects()->getContent().push_back(beforeCurrentItems.getObjects()->getContent()[i]);   
                }
                beforeCurrentItems.getObjects()->getContent().erase(beforeCurrentItems.getObjects()->getContent().begin() + i);
            }
        }
    }   
}

